package src.leetcode.everyday.mid_3446;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 李逸重
 * @CreateTime: 2025-08-28
 * @Description:
 * @Version: 1.0
 */
public class sortMatrix {
    public static void main(String[] args) {
        int[][] grid = {{1,7,3},{9,8,2},{4,5,6}};
        System.out.println(new Solution().sortMatrix(grid));
    }

}


class Solution {
    public int[][] sortMatrix(int[][] grid) {
        int n = grid.length;
        for (int i = 1 - n; i <= 0; i++) {
            List<Integer> list = new ArrayList<>();
            for (int j = 0; j - i < n; j++) {
                list.add(grid[j - i][j]);
            }
            list.sort(Integer::compareTo);
            for (int j = 0; j - i < n; j++) {
                grid[j - i][j] = list.get(list.size() - 1 - j);
            }
        }

        for (int i = 1; i < n; i++) {
            List<Integer> list = new ArrayList<>();
            for (int j = i; j - i < n && j < n; j++) {
                list.add(grid[j - i][j]);
            }
            list.sort(Integer::compareTo);
            for (int j = i; j - i < n && j < n; j++) {
                grid[j - i][j] = list.get(j - i);
            }
        }

        return grid;
    }
}